const app = getApp();
let rpn = require('../../utils/rpn.js');
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    index: 0,
    book_list: [{'book_id':0,'book_name':'不选择账本'}],
    date: '',
    textareaAValue: '',
    consume_type:1,
    nav:0,
    type_list: [],
    input_str:'',
    show:false,
    input_msg:'请输入',
    book_id:'', //账本id
    type_id:'', //消费类目id
    disabled:false,
  },

  onShow:function(){
    let _this = this;
    _this.getUserInfo(); //获取用户信息
    _this.getBookList(); //获取账本
    _this.init_date();  //默认显示当天
    _this.getConsumeList(); //获取种类信息
  },

  //获取个人信息
  getUserInfo:function(){
    let _this = this;
    wx.showLoading({
      title: '加载中',
    })
    app._post('api/member/getUserInfo', {}, function(result) {
      wx.hideLoading({})
      if(result.code == 200){
        _this.setData({
          is_login:true,
        })
      }else{
        wx.navigateTo({
          url: '../auth/auth'
        })
      }
    });
  },

  //默认获取当天
  init_date:function(){
    let _this = this;
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate;
    _this.setData({
      date:currentdate
    })
  },

  //检测是否是数字
  isNumber:function(val){
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    if(regPos.test(val)){
      return true;
    }else{
      return false;
    }
  },

  /*表单提交*/
  formSubmit: function(e) {
    let _this = this,
      values = e.detail.value,
      type_id = _this.data.type_id, //消费种类
      book_id = _this.data.book_id, //账本id
      input_msg = _this.data.input_msg, //金额
      date = _this.data.date, //日期
      trans_type = _this.data.consume_type, //消费or收入
      msg = '',
      show_msg = false,
      input_money = _this.isNumber(input_msg);
    // 表单验证
    if (type_id == ''){
      msg = '请选择类目';
      show_msg = true;
    }else if(input_msg == '请选择'){
      msg = '请输入金额';
      show_msg = true;
    }else if(!input_money){
      msg = '请输入金额';
      show_msg = true;
    }else if(input_msg == 0){
      msg = '金额不能为0';
      show_msg = true;
    }

    if(show_msg){
      wx.showToast({
        title: msg,
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    app._post('api/member/sub_bill',
      {
        type_id:type_id,
        book_id:book_id,
        trans_money:input_msg,
        tips:values.tips,
        trans_type:trans_type,
        trans_day:date
      }, 
    function(result) {
      if(result.code == 200){
        wx.showModal({
          title: '温馨提示',
          content: '记账成功，是否再记一笔？',
          cancelText:'前往首页',
          success (res) {
            if (res.confirm) {
              _this.setData({
                input_msg:0.00
              })
            } else if (res.cancel) {
              wx.redirectTo({
                url: '/pages/index/index',
              })
            }
          }
        })
      }else{
        wx.showToast({
          title: result.msg,
          icon: 'none',
          duration: 1500
        })
      }
    }, false, function() {
      // 解除禁用
      _this.setData({
        disabled: false
      });
    });
  },

  //点击高亮
  select_type:function(e){
    let _this = this,
     index = e.currentTarget.dataset.index,
     type_list = _this.data.type_list;
     for(var p in type_list){
      if(p == index){
        type_list[p].select = true;
      }else{
        type_list[p].select = false;
      }
     }
     _this.setData({
      type_list:type_list,
      type_id:type_list[index].consume_id
     })
  },

  //获取种类
  getConsumeList:function(){
    let _this = this,
      consume_type = _this.data.consume_type;
    app._post('api/member/getConsumeList', {consume_type:consume_type}, function(result) {
      _this.setData({
        type_list:result.data
      })
    }); 
  },

  //点击弹窗
  close_shadow:function(){
    let _this = this;
    _this.setData({
      show:false
    })
  },

  //弹出键盘
  keybord:function(){
    let _this = this;
    _this.setData({
      show:true
    })
  },

  //获取账本
  getBookList:function(){
    let _this = this,
      book_list = _this.data.book_list;
    app._post('api/member/getUserBookList', {}, function(result) {
      _this.setData({
        book_list:book_list.concat(result.data)
      })
    });
  },

  //种类选择
  tabSelect(e) {
    this.setData({
      consume_type: e.currentTarget.dataset.id,
    })
    this.getConsumeList();
  },

  //日期选择
  DateChange:function(e){
    let _this = this;
    _this.setData({
      date:e.detail.value
    })
  },

  //下拉框选择
  PickerChange(e) {
    let _this = this,
      book_list = _this.data.book_list,
      index = e.detail.value;
    _this.setData({
      index: index,
      book_id:book_list[index].book_id
    })
  },

  //输入金额
  input_money:function(e){
    console.log(e);
  },
  
  //输入框
  textareaAInput(e) {
    this.setData({
      textareaAValue: e.detail.value
    })
  },

  //计算器逻辑
  sub_count:function(){
    let _this = this,
      input_str = _this.data.input_str;
      input_str = rpn.calCommonExp(input_str).toFixed(2);
      if(input_str == 'NaN'){
        wx.showToast({
          title:'计算公式不正确哦',
          icon: 'none',
          duration: 1500
        })
      }else{
        _this.setData({
          input_str:'',
          input_msg:input_str,
          show:false
        })
      }
  },

  //输入字段
  view_this:function(e){
    let _this = this,
      input_str = _this.data.input_str,
      data_str = e.currentTarget.dataset.str;

      _this.setData({
        input_str:input_str+data_str
      })
  },

  //删除
  del_str:function(){
    let _this = this,
      input_str = _this.data.input_str,
      str = '';
      if(input_str.length>0){
        str = input_str.substring(0,input_str.length-1);
      }else{
        str = '';
      }
      _this.setData({
        input_str:str
      })
  },
})